﻿
using SuperSocket.Server;

namespace SvCore.Tcp;


public class MySession : AppSession
{
    public bool IsConnected = false;

    // 绑定唯一ID
    public long Puid = 0;

    // 绑定对象
    public object? Obj { get; set; } = null;
    public T? GetObj<T>() where T : class
    {
        if (Obj is null)
        {
            return null;
        }
        return (T)Obj;
    }

    public int Send<T>(T msg, EProtocolResult ecode = EProtocolResult.成功) where T : gpb.IMessage
    {
        var pack = MyPackage.From(msg, ecode);
        loger.Debug($"Send-> pid {pack.ProtocolId} {typeof(T).Name} code{pack.Ecode} code{pack.ECode}");
        //loger.Debug($"Send Body {pack.Body.Length} {pack.Body}");
        return Send(pack);
    }
    public int Send(MyPackage pack)
    {
        if (!IsConnected)
        {
            return 1;
        }
        (this as IAppSession).SendAsync(pack.Encode());
        return 0;
    }
}
